package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Comment;
import ex.com.jm.food.service.CompanyCommentService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class CompanyCommentAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;

	private CompanyCommentService companyCommentService;
	private Comment comment;
	private String comId;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String s_date;
	private String e_date;
	private String order;
	private String sort;
	private String repment;
	private String userName;
	private String cmentId;
	private Map<String, String> dateMap;
	public CompanyCommentService getCompanyCommentService() {
		return companyCommentService;
	}
	public void setCompanyCommentService(CompanyCommentService companyCommentService) {
		this.companyCommentService = companyCommentService;
	}
	public Comment getComment() {
		return comment;
	}
	public void setComment(Comment comment) {
		this.comment = comment;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getRepment() {
		return repment;
	}
	public void setRepment(String repment) {
		this.repment = repment;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getCmentId() {
		return cmentId;
	}
	public void setCmentId(String cmentId) {
		this.cmentId = cmentId;
	}
	/**
	 * 列表页面
	 * @return
	 */
	public String comentList(){
		dateMap=DateUtils.getDate();
		return "comentList";
	}
	
	public void getComentList(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<Comment> comments=this.getCompanyCommentService().getComments(offset, pageRow, comId, userName, s_date, e_date, order, sort);
	    JSONArray jocoment=JSONArray.fromObject(comments);
	    totalCount=this.getCompanyCommentService().commentCount(comId, userName, s_date, e_date);
	    JSONObject jo=JsonUtils.buildJson(jocoment, initPage, pageRow, totalCount);
	    PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 编辑
	 * @return
	 */
	public String editComment(){
	     if(cmentId!=null){
	    	 comment=this.getCompanyCommentService().editComment(cmentId);
	     }
	     System.out.println(comment.getCmentValue());
		return "edit";
	}
	
	public String updateComment(){
		boolean flag=this.getCompanyCommentService().updateComment(cmentId, repment, DateUtils.getCurrentDate());
		if(flag==true){
			return "list";
		}
		return "list";
	}
}
